package com.huizhan.web.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.view.*;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 账号api
 */
@Api(tags = "Account",description = "账号")
@RestController
public class AccountController {
    
    @Autowired
    private AccountDao dao;
   
    /**
     * 获取账号集合
     * @return api结果
     */
    @ApiOperation(value = "获取账号集合")
    @RequestMapping(value =  "/api/Account/",method = {RequestMethod.GET})
    public ApiResult<List<Account>> Get(){
         ApiResult<List<Account>> result=new ApiResult<List<Account>>();
        try 
        {
            List<Account> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取账号信息
     * @param id 账号ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取账号信息")
    @RequestMapping(value =  "/api/Account/{id}",method = {RequestMethod.GET})
    public ApiResult<Account> Get(@PathVariable String id){
        ApiResult<Account> result=new ApiResult<Account>();
        try
        {
            Account model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


    /**
     * 根据手机号获取账号信息
     * @param phone 手机号
     * @return api结果
     */
    @ApiOperation(value = "根据手机号获取账号信息")
    @RequestMapping(value =  "/api/Account/Phone/{phone}",method = {RequestMethod.GET})
    public ApiResult<Account> Phone(@PathVariable String phone){
        ApiResult<Account> result=new ApiResult<Account>();
        try
        {
            Account model= dao.findByPhone(phone);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改账号信息
     * @param id 账号ID
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "修改账号信息")
    @RequestMapping(value =  "/api/Account/{id}",method = {RequestMethod.PUT})
    public ApiResult<Account> Edit(@PathVariable String id,@RequestBody Account model){
       ApiResult<Account> result=new ApiResult<Account>();
        try
        {
            Account account= dao.getOne(id);

            if(account==null){
               result.setMessage("参数错误");
               result.setHasError(true);
            }else {
                model.setLastDate(new Date());
                model.setAccountId(id);                
                if(model.getOpenId()==""){
                   model.setOpenId(account.getOpenId());
                }
                model.setCreateDate(account.getCreateDate());
                dao.save(model);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


    /**
     * 修改账号当前项目信息
     * @param id ID
     * @param project 项目
     * @param projectId 项目ID
     * @return api结果
     */
    @ApiOperation(value = "修改账号当前项目信息")
    @RequestMapping(value =  "/api/Account/Project/{id}/{project}/{projectId}",method = {RequestMethod.GET})
    public ApiResult<Account> Project(@PathVariable String id,@PathVariable String project,@PathVariable String projectId){
       ApiResult<Account> result=new ApiResult<Account>();
        try
        {
            Account account= dao.getOne(id);

            if(account==null){
               result.setMessage("参数错误");
               result.setHasError(true);
            }else {
                account.setProject(project);
                account.setProjectId(projectId);    
                dao.save(account);
                result.setData(account);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


    /**
     * 查询账号信息集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询账号信息")
    @RequestMapping(value =  "/api/Account/Search",method = {RequestMethod.POST})
    public ApiResult<List<Account>> Search(@RequestBody PagePhone model){
         ApiResult<List<Account>> result=new ApiResult<List<Account>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"account_Id") ;            
            Page<Account> page = dao.findByWhere(model.getName(),model.getPhone(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 更新密码
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "更新密码")
    @RequestMapping(value =  "/api/Account/Password",method = {RequestMethod.POST})
    public ApiResult<Password> Password(@RequestBody Password model){
         ApiResult<Password> result=new ApiResult<Password>();
         
        try
        {    
            Account account=dao.getOne(model.getId());

            if(account==null){
                result.setHasError(true);
                result.setMessage("没有该用户");
            }else if(!account.getPassword().equals(model.getPassword())){
                result.setHasError(true);
                result.setMessage("原密码错误");
            }else if(!model.getCheckPass().equals(model.getNewPassword())){
                result.setHasError(true);
                result.setMessage("两次密码不一致");
            }else{
                account.setPassword(model.getNewPassword());
                
                dao.save(account);
            }

            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 更新状态
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "更新状态")
    @RequestMapping(value =  "/api/Account/State",method = {RequestMethod.PATCH})
    public ApiResult<State> State(@RequestBody State model){
         ApiResult<State> result=new ApiResult<State>();
         
        try
        {    
            Account account=dao.getOne(model.getId());

            if(account==null){
                result.setHasError(true);
                result.setMessage("没有该用户");
            }else{
                account.setEnabled(model.getEnabled());
                
                dao.save(account);
            }

            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 添加账号信息
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "添加账号信息")
    @RequestMapping(value =  "/api/Account/",method = {RequestMethod.POST})
    public ApiResult<Account> Add(@RequestBody Account model){
         ApiResult<Account> result=new ApiResult<Account>();
        try
        {
            if(dao.findByPhone(model.getPhone())!=null){
                result.setHasError(true);
                result.setMessage("手机号码已存在");
            }else{
                model.setAccountId(UUID.randomUUID().toString());
                model.setCreateDate(new Date());
                model.setLastDate(new Date());
    
                dao.save(model);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除账号信息
     * @param id 账号ID
     * @return api结果
     */
    @ApiOperation(value = "删除账号信息")
    @RequestMapping(value =  "/api/Account/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  String id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    
    /**
     * 登录
     * @param phone 账号名
     * @param password 密码
     * @return api结果
     */
    @ApiOperation(value = "登录")
    @RequestMapping(value =  "/api/Account/Login/{phone}/{password}",method = {RequestMethod.GET})
    public ApiResult<Account> Login(@PathVariable String phone,@PathVariable String password){
        ApiResult<Account> result=new ApiResult<Account>();
        try
        {
            Account model= dao.findByPhoneAndPassword(phone,password);

            if(model==null){
                result.setHasError(true);
                result.setMessage("用户名密码错误");
            }else if(model.getEnabled()==0){
                result.setHasError(true);
                result.setMessage("账号已禁用");
            }else{
                result.setData(model);
                model.setLastDate(new Date());
                dao.save(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }
}

